package com.genesis.yunnanji.activity;

import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.genesis.yunnanji.R;
import com.genesis.yunnanji.bean.PavilionDetailsBean;
import com.genesis.yunnanji.config.GenesisApiConfig;
import com.jaeger.library.StatusBarUtil;
import com.lidroid.xutils.BitmapUtils;

import org.xutils.view.annotation.ViewInject;


public class PavilionIntro extends BaseActivity {
    @ViewInject(R.id.tb_pavilionintor_toolbar)
    private Toolbar mToolbar;
    @ViewInject(R.id.iv_pavilionintro_bg)
    private ImageView ivBackdrop;
    @ViewInject(R.id.tv_pavilionintro_title)
    private TextView tvTitle;
    @ViewInject(R.id.tv_pavilionintro_name)
    private TextView tvName;
    @ViewInject(R.id.web_pavilionintro_web)
    private WebView webView;
    private PavilionDetailsBean detailsBean;
    private LinearLayout mLinearLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState, R.layout.activity_pavilionintro);
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        mLinearLayout = (LinearLayout) findViewById(R.id.ll_pavilionintron_parent);
        detailsBean = (PavilionDetailsBean) getIntent().getSerializableExtra("pavilion_bean");
        mToolbar.setTitle("");
        setSupportActionBar(mToolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        StatusBarUtil.setColor(this, getResources().getColor(R.color.main_color), 2);

    }

    @Override
    protected void setUpView() {
        setupWebView();
        tvTitle.setText(detailsBean.getResult().getInfo().getTitle());
        tvName.setText(detailsBean.getResult().getInfo().getTitle() + "\n" + detailsBean.getResult().getInfo().getCode());
        BitmapUtils bitmapUtils = new BitmapUtils(context);
        bitmapUtils.display(ivBackdrop, GenesisApiConfig.PIC_HOST + detailsBean.getResult().getInfo().getCover_img());
        webView.loadUrl(detailsBean.getResult().getInfo().getTarget_link() + "?from=app");

    }

    public void setupWebView() {
        webView.setWebViewClient(new mWebClient());
        webView.getSettings().setUseWideViewPort(true);
        webView.getSettings().setAllowFileAccess(true);
        webView.getSettings().setDomStorageEnabled(true);
        webView.getSettings().setAppCacheEnabled(false);
        webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
        webView.getSettings().setSupportZoom(true);
        webView.getSettings().setBuiltInZoomControls(true);
        webView.getSettings().setDefaultFontSize(12);
        webView.getSettings().setLoadWithOverviewMode(true);
        webView.getSettings().setAllowFileAccess(true);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
        webView.getSettings().setDatabaseEnabled(true);
        webView.getSettings().setGeolocationEnabled(true);
    }

    private class mWebClient extends WebViewClient {

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
        }
    }
}
